package com.atguigu.dga.governance.service.impl;

import com.atguigu.dga.constant.DgaConstant;
import com.atguigu.dga.governance.bean.GovernanceAssessTable;
import com.atguigu.dga.governance.bean.GovernanceType;
import com.atguigu.dga.governance.mapper.GovernanceAssessTableMapper;
import com.atguigu.dga.governance.service.GovernanceAssessTableService;
import com.atguigu.dga.governance.service.GovernanceTypeService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 表治理考评情况 服务实现类
 * </p>
 *
 * @author wuzhida
 * @since 2024-07-02
 */
@Service
@DS("dga")
public class GovernanceAssessTableServiceImpl extends ServiceImpl<GovernanceAssessTableMapper, GovernanceAssessTable> implements GovernanceAssessTableService {
    
    @Autowired
    private GovernanceTypeService governanceTypeService;

    /**
     * 计算表级分数
     * @param assessDate
     */
    @Override
    public void calcTableScore(String assessDate) {
        //删除当前考评日期的数据
        remove(
                new QueryWrapper<GovernanceAssessTable>()
                        .eq("assess_date" , assessDate)
        );
        
        List<GovernanceAssessTable> tableScoreList = getBaseMapper().getTableScore(assessDate);
        //获取板块的权重
        List<GovernanceType> governanceTypeList = governanceTypeService.list();
        //处理成Map结构
        // K : 指标类型
        // V : 权重
        Map<String, BigDecimal> governanceTypeMap = new HashMap<>();
        for (GovernanceType governanceType : governanceTypeList) {
            governanceTypeMap.put(governanceType.getTypeCode(), governanceType.getTypeWeight());
        }

        for (GovernanceAssessTable governanceAssessTable : tableScoreList) {
            BigDecimal specScore = governanceAssessTable.getScoreSpecAvg().multiply(governanceTypeMap.get(DgaConstant.METRIC_TYPE_SPEC)).divide(BigDecimal.TEN);
            BigDecimal storageScore = governanceAssessTable.getScoreStorageAvg().multiply(governanceTypeMap.get(DgaConstant.METRIC_TYPE_STORAGE)).divide(BigDecimal.TEN);
            BigDecimal calcScore = governanceAssessTable.getScoreCalcAvg().multiply(governanceTypeMap.get(DgaConstant.METRIC_TYPE_CALC)).divide(BigDecimal.TEN);
            BigDecimal qualityScore = governanceAssessTable.getScoreQualityAvg().multiply(governanceTypeMap.get(DgaConstant.METRIC_TYPE_QUALITY)).divide(BigDecimal.TEN);
            BigDecimal securityScore = governanceAssessTable.getScoreSecurityAvg().multiply(governanceTypeMap.get(DgaConstant.METRIC_TYPE_SECURITY)).divide(BigDecimal.TEN);
            //加权总分
            BigDecimal scoreOnTypeWeight = specScore.add(storageScore).add(calcScore).add(qualityScore).add(securityScore);
            governanceAssessTable.setScoreOnTypeWeight(scoreOnTypeWeight);
        }
        saveBatch(tableScoreList);
    }
}
